/*
 * Copyright (c) 2012-2013 Open Source Community - <http://www.peerfact.org>
 * Copyright (c) 2011-2012 University of Paderborn - UPB
 * Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
 *
 * This file is part of PeerfactSim.KOM.
 * 
 * PeerfactSim.KOM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 * 
 * PeerfactSim.KOM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PeerfactSim.KOM.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package org.peerfact.impl.overlay.dht.pastry.parallel.messages;

import org.peerfact.Constants;
import org.peerfact.impl.overlay.dht.pastry.base.components.PastryContact;
import org.peerfact.impl.overlay.dht.pastry.base.components.PastryID;
import org.peerfact.impl.overlay.dht.pastry.base.messages.PastryBaseMsg;
import org.peerfact.impl.overlay.dht.pastry.base.nodestate.RouteSet;

// implemented by Martin Wiegand.

public class IterativeUpdateMsg extends PastryBaseMsg {

	private static final long serialVersionUID = 2090243054780746682L;

	private PastryContact senderContact;

	private PastryID lookupKey;

	private RouteSet rs;

	private int operationID;

	private int hops;

	// this flag can be used if an implementation of iterative value lookups is
	// desired
	private boolean valueLookup;

	public IterativeUpdateMsg(PastryContact sender, PastryContact receiver,
			PastryID target, RouteSet rs, int lookupId, int hops,
			boolean valueLookup) {
		super(sender.getOverlayID(), receiver.getOverlayID());
		senderContact = sender;
		lookupKey = target;
		this.rs = rs;
		operationID = lookupId;
		this.hops = hops;
		this.valueLookup = valueLookup;
	}

	/**
	 * returns the transmission size of this message
	 * 
	 * @return transmission size
	 */
	@Override
	public long getSize() {
		return super.getSize() + lookupKey.getTransmissionSize()
				+ senderContact.getTransmissionSize() + 2 * Constants.INT_SIZE
				+ rs.getTransmissionSize() + Constants.BOOLEAN_SIZE;
	}

	/**
	 * @return The contact of the initial sender of this message
	 */
	public PastryContact getSenderContact() {
		return senderContact;
	}

	/**
	 * @return The target id for which the node is looked up
	 */
	public PastryID getLookupKey() {
		return lookupKey;
	}

	/**
	 * @return The operation id of the lookup operation
	 */
	public int getOperationID() {
		return operationID;
	}

	/**
	 * @return The number of hops the message has done
	 */
	public int getHops() {
		return hops;
	}

	/**
	 * Increments the hop counter by 1
	 */
	public void incrementHopCount() {
		hops++;
	}

	/**
	 * @return the RouteSet
	 */
	public RouteSet getRouteSet() {
		return rs;
	}

	public boolean getValueLookup() {
		return valueLookup;
	}

}
